package com.visionork.android.tai.model.adapter;

import java.net.URL;
import java.util.ArrayList;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.visionork.android.tai.model.Video;

public class VideoAdapter extends TaiAdapterBase {

	public VideoAdapter(String urlLink) {
		super(urlLink);
	}

	@Override
	public ArrayList<?> getData() {
		ArrayList<Video> videos = new ArrayList<Video>();
		try {
			Document doc = Jsoup.parse(new URL(urlLink), TIME_OUT);

			Elements defaultElms = doc.getElementsByTag("span");
			for (Element elm : defaultElms) {
				if (elm.hasAttr("class")
						&& elm.attr("class").equals("contentlist")
						&& elm.hasText()) {
					String href = urlLink;
					String title = elm.text().trim();
					Video video = new Video();
					video.setTitle(title);
					video.setUrlLink(href);
					videos.add(video);
				}
			}

			Elements elms = doc.getElementsByTag("a");
			for (Element elm : elms) {
				if (elm.hasAttr("class")
						&& elm.attr("class").equals("contentlist")
						&& elm.hasText()) {
					String href = elm.attr("href");
					String title = elm.text().trim();
					Video video = new Video();
					video.setTitle(title);
					video.setUrlLink(href);
					videos.add(video);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return videos;
	}

}
